package org.czl.leetcode2;

public class l2516 {
    public int takeCharacters(String s, int k) {
        char[] ss = s.toCharArray();
        //这里直接就对遍历后的数组进行-k的操作了
        int[] c = new int[]{-k,-k,-k};
        //遍历字符串中 a b c 字符串的数量
        for(char i : ss){
            c[i - 'a']++;
        }
        for(int x : c) if(x < 0)return -1;
        int ans = 0;
        for(int right = 0, left = 0; right < ss.length;right++){
            char temp = ss[right];
            c[temp - 'a']--;
            if(check(c)){
                ans = Math.max(right - left + 1, ans);
            }
            while(!check(c) && left <= right){
                char ret = ss[left++];
                c[ret - 'a']++;
            }
        }
        return ss.length - ans;
    }
    boolean check(int[] c){
        for(int i : c){
            if(i < 0)return false;
        }
        return true;
    }
}
